package cn.yqx.bankshop.order.controller;

import cn.yqx.bankshop.order.service.OrderService;
import cn.yqx.bankshop.result.Result;
import cn.yqx.bankshop.result.ResultCode;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/v1/statistical")
public class StatisticalOrderController {
    private static final String COMMODITY_KEY = "commodity"  ;
    private static final String SALESERVICE_KEY ="saleService" ;
    private static final String LOGISTICALSERVICE_KEY = "logisticalService" ;
    private static final String OVERMERIT_KEY = "overMerit" ;

    @Autowired
    OrderService orderService ;

    @RequestMapping(value = "/find_suppliers",method = RequestMethod.GET)
    public Object findSuppliersBySales (){
        Result result  ;
        try{
            result = Result.success() ;
            List<Map<String,Object>> list = orderService.findSuppliersBySales();
            result.setData(list);
        }catch (Exception e){
            result = Result.failed(ResultCode.SYSTEM_EXECUTION_ERROR) ;
            log.info(" error : {} " , e);
        }
        return  result ;
    }

    @RequestMapping(value = "find_goods_top5" ,method = RequestMethod.GET)
    public Object findGoodsTop5(Long supplierId){
        List<Map<String,Object>> list = orderService.findGoodsTop5(supplierId) ;
        return list ;
    }

    @RequestMapping(value = "find_evaluate_by_supplierId",method = RequestMethod.GET)
    public Object findEvaluateBySupplier(Long supplierId){
        Result result  ;
        try{
            List<Map<String,Object>> list = orderService.findEvaluateBySupplier(supplierId) ;
            result = Result.success() ;

            if(!list.isEmpty()){
                Map<String,Object> map = list.get(0) ;
                if(map!=null && map.get(COMMODITY_KEY) !=null  ){
                    result.setData(list.get(0));
                    return result ;
                }
            }
            JSONObject json = new JSONObject() ;
            Double score = 5.0 ;
            json.put(COMMODITY_KEY,score);
            json.put(SALESERVICE_KEY,score);
            json.put(LOGISTICALSERVICE_KEY,score);
            json.put(OVERMERIT_KEY,score) ;
            result.setData(json);

        }catch (Exception e){
                result = Result.failed(ResultCode.SYSTEM_EXECUTION_ERROR) ;
                log.info(" error : {} ",e);
        }
        return result ;
    }

    @RequestMapping(value = "/find_goods_by_sales" ,method = RequestMethod.GET)
    public Object findGoodsBySales(){

        List<Map<String,Object>> goods = orderService.findGoodsBySales();
        return goods ;

    }
    @RequestMapping(value = "/find_orders_by_status",method = RequestMethod.GET)
    public Object findOrdersByStatus(Long userId , String timeType ){
        Result result  ;
        try{
            result = Result.success() ;

        }catch (Exception e){
            result = Result.failed(ResultCode.SYSTEM_EXECUTION_ERROR)  ;
            log.error(" error : {}" ,e);
        }
        return result;
    }

    /**
     * 消费统计
     * @param userId 用户账号
     * @return result
     */
    @RequestMapping(value = "consumption_by_userid",method = RequestMethod.GET)
    public Object consumptionByUserid(Long userId){
        Result result  ;
        try{
            result = Result.success() ;
            Map<String,Object> map = orderService.consumptionByUserid(userId) ;
            result.setData(map);
        }catch (Exception e){
            result = Result.failed(ResultCode.SYSTEM_EXECUTION_ERROR)  ;
            log.error(" error : {}" ,e);
        }
        return result;
    }
}
